<template>
	<!-- 外层容器 -->
	<view class="search-container">
		<!-- 输入框容器 -->
		<view class="input-container">
			<input class="ipt_search" :placeholder="placeholderTip" :value="inputVal" @input="onSearchDataChange"
				placeholder-style="color:#eaeaea" />
		</view>

		<!-- 图标容器 -->
		<view class="icon-container">
			<uni-icons type="search" size="20" color="#2196f3" @click="search"></uni-icons>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { defineProps, defineEmits } from 'vue'

	/**
	 * 组件 Props 定义
	 */
	const props = defineProps({
		// 占位文本
		placeholderTip: {
			type: String,
			default: '搜索点什么...'
		},
		// 输入框内容
		inputVal: {
			type: String,
			default: ''
		}
	})

	/**
	 * 组件事件定义
	 * 
	 * 使用时可以用 v-model:inputVal 进行双向绑定
	 * 或者直接监听 @update:inputVal
	 */
	const emits = defineEmits(['update:inputVal'])

	/**
	 * 输入框内容改变触发
	 * e.detail.value 为 input 的最新内容
	 */
	function onSearchDataChange(e : any) {
		emits('update:inputVal', e.detail.value)
	}

	function search() {
		emits('search')
	}
</script>

<style lang="scss" scoped>
	/* 外层容器 */
	.search-container {
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: rgba(255, 255, 255, 0.2);
		margin: 15px 20px 5px;
		border-radius: 50px;
		height: 40px;
		box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
		border: 1px solid rgba(255, 255, 255, 0.5);
		transition: all 0.3s ease;

		&:focus-within {
			box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
			background-color: rgba(255, 255, 255, 0.3);
		}
	}

	/* 输入框容器 */
	.input-container {
		width: 100%;
		font-size: 14px;
		color: white;
		margin-left: 15px;
	}

	/* 图标容器 */
	.icon-container {
		background-color: white;
		border-radius: 50%;
		padding: 5px 7px;
		height: 30px;
		width: 30px;
		display: flex;
		align-items: center;
		justify-content: center;
		box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
	}

	/* 输入框样式 */
	.ipt_search {
		width: 100%;
		background-color: transparent;
		border: none;
		color: white;
		outline: none;
		font-size: 14px;
	}
</style>